//
//  findDriveListVC.m
//  tcpoctest
//
//  Created by gump on 11/23/12.
//  Copyright (c) 2012 gump. All rights reserved.
//

#import "findDriveListVC.h"
#import "PersonCell.h"
#import "NearPeopleInfo.h"

@interface findDriveListVC ()

@end

@implementation findDriveListVC

- (id)init
{
    self = [super init];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)initUI
{
    self.view.backgroundColor = VIEW_BG_COLOR;
    
    [self createTitle];
    self.mtitle.text = @"附近的车";
    
    UIBarButtonItem *backItem = [self createBarBackButtonItemWithTitle:@"返回" target:self action:@selector(backButtonPressed)];
    self.navItem.leftBarButtonItem = backItem;
    
    mtable =[[UITableView alloc]initWithFrame:CGRectMake(0, TOPBAR_HIGHT, WINDOW_WIDTH, WINDOW_HIGTH - TOPBAR_HIGHT - 20)
                                        style:UITableViewStylePlain];
    mtable.backgroundColor=CELL_BG_COLOR;
    mtable.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
    mtable.delegate =self;
    mtable.dataSource =self;
    [self.view insertSubview:mtable belowSubview:self.mtitle];
    
}

#pragma mark - button selector -
-(void)backButtonPressed
{
    [[ViewManager sharedViewManager] backView:self];
}

#pragma mark - private fun-
-(void)reloadTable
{
    [mtable reloadData];
}

#pragma mark - tableView DataSource -
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==0)
    {
        return [DataControl shareControl].findDriveShowArr.count;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger section=[indexPath section];
    NSUInteger row = [indexPath row];
    static NSString *CellIdentifier = @"Cell";
    PersonCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[PersonCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
		cell.selectionStyle = UITableViewCellSelectionStyleGray;
    }
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    switch (section)
    {
        case 0:
        
            {
                NSString *mail = [[DataControl shareControl].findDriveShowArr objectAtIndex:row];
                
                NearPeopleInfo *npi = [[DataControl shareControl].findDriveDic objectForKey:mail ];
                
                FirendInfo *fi = [[DataControl shareControl] SearchInfo:mail ];
                NSString *url = [[DataControl shareControl] SearchHeadImage:mail];
                if (fi != nil)
                {
                    [[ViewManager sharedViewManager] dismissProgressHUD];
                    cell.nameLabel.text = fi.name;
                    if (fi.sex == 1) {
                        cell.sexicon.image = [UIImage imageNamed:@"icon_male.png"];
                    }else{
                        cell.sexicon.image = [UIImage imageNamed:@"icon_female.png"];
                    }
                    if ([url length] > 0)
                    {
                        cell.imageView.image = [UIImage imageNamed:@"default_head.png"];
                        UIImage *headImage = [UIImage imageWithContentsOfFile:url];
                        if (headImage) {
                            cell.imageView.image = nil;
                            cell.headImageView.image = [UIImage imageWithContentsOfFile:url];
                        }
                    }else{
                        cell.headImageView.image = nil;
                        cell.imageView.image = [UIImage imageNamed:@"default_head.png"];
                    }
                }else{
                    cell.imageView.image = [UIImage imageNamed:@"default_head.png"];
                }
                cell.disLabel.text = [NSString stringWithFormat:@"%d米内", npi.distance * 100];
            }
            cell.selectionStyle=UITableViewCellSelectionStyleNone;

            break;
        default:
            break;
    }
    
    return cell;
}

#pragma mark -table delegate-
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    switch ([indexPath section]) {
        case 0:
        {
            int row = [indexPath row];
            if (row >=0 && row < [[DataControl shareControl].findDriveShowArr count])
            {
                NSString *mail = [[DataControl shareControl].findDriveShowArr objectAtIndex:row];
                FirendInfo *fi = [[DataControl shareControl] SearchInfo:mail];
                if(fi != nil)
                {
                    [[DataControl shareControl] setCurrentFI:fi];
                    [[ViewManager sharedViewManager] showView:VI_PERSONINFO
                                             withIsRemoveCurV:false
                                                withDirection:VD_RIGHT];
                }
            }
        }
            break;
            
        default:
            break;
    }
}

@end
